/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  ImmediateViewController.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#import "ImmediateViewController.h"

@implementation ImmediateViewController
@synthesize cameraViewUART0, cameraViewUART1, joypad, leftBlackfin, rightBlackfin, configuration;

// rebuild a single image in portrait mode
-(void)rebuildForPortraitOrientation {	
	// get screen resolution
	CGRect screenRect = [[UIScreen mainScreen] bounds];
	
	// adjust image resolution from the blackfin to best suit the camera view resolution
	[cameraViewUART0 autoAdjustResolution:screenRect];
	
	// adjust the camera view screen format
	[cameraViewUART0 setFrame:screenRect];
	
}

// rebuild two images in landscape mode
-(void)rebuildForLandscapeOrientation {
	// get screen resolution and create side by side zones
	CGRect screenRect = [[UIScreen mainScreen] bounds]; // gives us prortrait!
	CGRect imageRectLeft = CGRectMake(0.0, 0.0, screenRect.size.height/2, screenRect.size.width); 
	CGRect imageRectRight = CGRectMake(screenRect.size.height/2, 0.0, screenRect.size.height/2, screenRect.size.width); 	

	// adjust the image resolution from the blackfins to best suit the camera view resolution
	[cameraViewUART0 autoAdjustResolution:imageRectLeft];
	[cameraViewUART1 autoAdjustResolution:imageRectRight];
	
	// adjust the camera views into the respective zones.
	[cameraViewUART0 setFrame:imageRectLeft];
	[cameraViewUART1 setFrame:imageRectRight];
	[self.view addSubview:cameraViewUART1];	
}

// update for current orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
	if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ) {
		[self rebuildForLandscapeOrientation];
	} else {
		[self rebuildForPortraitOrientation];
	}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	[super viewDidUnload];
	leftBlackfin=nil;
	rightBlackfin=nil;	
	cameraViewUART0=nil;
	cameraViewUART1=nil;
	joypad=nil;
}

- (void)dealloc {
    [super dealloc];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated {
	cameraViewUART0.blackfinController=leftBlackfin;
	cameraViewUART1.blackfinController=rightBlackfin;

	[self rebuildForPortraitOrientation];

	[cameraViewUART0 startImageFeed];
	[cameraViewUART1 startImageFeed];
}

-(void)viewWillDisappear:(BOOL)animated {
	[cameraViewUART0 stopImageFeed]; 
	[cameraViewUART1 stopImageFeed];	
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}

@end
